package org.course.weather.controller;

import jakarta.validation.Valid;
import org.course.weather.entity.JsonResult;
import org.course.weather.entity.WeatherBean;
import org.course.weather.service.WeatherService;
import org.course.weather.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/weather")
public class WeatherController {
    @Autowired
    private WeatherService weatherService;

    @RequestMapping("/all")
    @ResponseBody
    public JsonResult<Object> getAllWeather() {
        List<WeatherBean> res = weatherService.getAllWeather();
        return new JsonResult<>(res);
    }

    @RequestMapping("/update")
    @ResponseBody
    public JsonResult<Object> updateCityWeather(@Valid @RequestBody WeatherBean reqWeather) {
        weatherService.UpdateCityWeather(reqWeather);
        return new JsonResult<>(new HashMap<String, String>(){{
            put("city", reqWeather.getCity());
            put("update_time", MyUtil.DTStr(reqWeather.getUpdate_time()));
        }});
    }
}
